<?php

namespace App\Services\Chat;


use App\Event\MicrophoneNotify;
use App\Exception\MessageException;
use App\Model\ChatRoom;
use App\Model\ChatRoomMicrophone;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\EventDispatcher\EventDispatcherInterface;

class MicroPhoneService
{
    #[Inject]
    protected EventDispatcherInterface $eventDispatcher;

    /**
     * Note - 锁定\解锁麦克风
     * DateTime 2024/9/3 19:29
     * User Admin
     * @param $params
     * @return int
     */
    public function lock($params)
    {
        $room_number = $params['room_number'];
        $roomId = ChatRoom::query()->where('room_number', $room_number)->value('room_id');
        $miSeq = $params['mi_seq'];
        $lock = $params['mi_lock'];// 0-正常；1-锁定
        $uid = $params['uid'];
        $result = ChatRoomMicrophone::query()->where('room_number', $room_number)->where('mi_seq', $miSeq)->update(['mi_seq_lock' => $lock]);
        if ($result) {
            // $microInfo = ChatRoomMicrophone::query()->where('room_id', $roomId)->where('mi_seq', $miSeq)->first();
            // $microInfo = modelToArray($microInfo);
            // $userInfo = [];
            // if ($microInfo['uid']) {
            //     $userInfo = User::query()->where('id', $microInfo['uid'])->first(['nickname', 'head_image', 'sex', 'age', 'noble', 'wealth', 'id']);
            //     $userInfo['wealth'] = getWealthLevel($userInfo['wealth']);
            // }
            $data = $this->getMicUserInfo($room_number, $miSeq);
            $microInfo = $data['microInfo'];
            $microInfo['room_id'] = $roomId;
            $userInfo = $data['userInfo'];
            $this->eventDispatcher->dispatch(new MicrophoneNotify($microInfo, $userInfo, $uid));
        }
        return $result;
    }

    /**
     * Note - 禁用\开启麦克风语音声音
     * DateTime 2024/9/4 15:36
     * User Admin
     * @param $params
     * @return int
     */
    public function voice($params)
    {
        $room_number = $params['room_number'];
        $roomId = ChatRoom::query()->where('room_number', $room_number)->value('room_id');
        $miSeq = $params['mi_seq'];
        $voice = $params['mi_voice'];// 1-关麦；2-开麦
        $oType = $params['o_type'];// 1-管理员；2-用户
        $uid = $params['uid'];
        $micro = ChatRoomMicrophone::query()->where('room_number', $room_number)->where('mi_seq', $miSeq)->first();
        if (!$micro) {
            throw new MessageException('麦位不存在');
        }
        if ($oType == 1) {
            $micro->a_voice_status = $voice;
        } else {
            $micro->u_voice_status = $voice;
        }
        $result = $micro->save();
        if ($result) {
            $data = $this->getMicUserInfo($roomId, $miSeq);
            $microInfo = $data['microInfo'];
            $microInfo['room_id'] = $roomId;
            $userInfo = $data['userInfo'];
            $this->eventDispatcher->dispatch(new MicrophoneNotify($microInfo, $userInfo, $uid));
        }
        return $result;
    }

    /**
     * Note - 获取麦位信息
     * DateTime 2024/9/3 19:29
     * User Admin
     * @param $room_number
     * @param null $miSeq
     * @return array
     */
    public function getMiInfo($room_number, $miSeq = null)
    {
        return ChatRoomMicrophone::query()->with('userInfo')->where('room_number', $room_number)->when($miSeq, function ($query) use ($miSeq) {
            return $query->where('mi_seq', $miSeq);
        })->orderBy('mi_seq')->get()->toArray();
    }

    /**
     * Note - 获取麦位用户信息
     * DateTime 2024/9/11 12:18
     * User Admin
     * @param $room_number
     * @param null $miSeq
     * @return array
     * [
     *     ['microInfo'] => []
     *     ['userInfo'] => []
     * ]
     */
    public function getMicUserInfo($room_number, $miSeq = null)
    {
        $query = ChatRoomMicrophone::query()->from('tb_chat_room_microphone AS m')
            ->leftJoin('tb_user As u', 'u.id', '=', 'm.uid')
            ->where('m.room_number', $room_number)
            ->orderBy('m.mi_seq');
        if (!is_null($miSeq)) {
            $data = $query->where('m.mi_seq', $miSeq)
                ->first(['m.mi_seq', 'm.mi_seq_lock', 'm.mi_up_down', 'm.u_voice_status', 'm.a_voice_status', 'm.room_number', 'u.nickname', 'u.head_image', 'u.sex', 'u.age', 'u.noble', 'u.wealth',
                    Db::raw('u.id AS uid')]);
            $data = modelToArray($data);
            $data['microInfo'] = [
                'room_number' => $data['room_number'],
                'mi_seq' => $data['mi_seq'],
                'mi_seq_lock' => $data['mi_seq_lock'],
                'mi_up_down' => $data['mi_up_down'],
                'u_voice_status' => $data['u_voice_status'],
                'a_voice_status' => $data['a_voice_status']
            ];
            $data['userInfo'] = [
                'nickname' => $data['nickname'],
                'head_image' => $data['head_image'],
                'sex' => $data['sex'],
                'age' => $data['age'],
                'noble' => $data['noble'],
                'wealth' => getWealthLevel($data['wealth']),
                'uid' => $data['uid']
            ];
        } else {
            $data = $query->get(['m.mi_seq', 'm.mi_seq_lock', 'm.mi_up_down', 'm.u_voice_status', 'm.a_voice_status', 'm.room_number', 'u.nickname', 'u.head_image', 'u.sex', 'u.age', 'u.noble', 'u.wealth',
                Db::raw('u.id AS uid')]);
            $data = modelToArray($data);
        }

        return $data;
    }

}